<?php
defined('ENTER_ID') or die('Hacking attempt');
/****************************
 * 功能：路由调度
 * 时间：2017/05/16
 * 作者：300js
 ****************************/
class Dispatcher
{
    /**
     * 运行
     */
    public static function run()
    {
        self::_dispatch();
        self::_call();
    }

    /**
     * 调用方法 区分大小写
     */
    private static function _call()
    {
        $sep = C('_sep');
        $dir = CONTROLLER_PATH;
        $paths = explode($sep, trim(__INFO__, $sep));
        $class = ucfirst($paths[0]) . C('_controller');
        $function = $paths[1];
        $path = $dir . $class . EXT;
        if (is_file($path)) {
            if (!class_exists($class)) {
                show_message('Class:' . $class . ' Not found');
            }
            $object = new $class;
            if (!method_exists($object, $function)) {
                show_message('Method:' . $function . ' Not found');
            } else {
                $param_array = self::_doParams($paths);
                $result = call_user_func_array(array($object, $function), $param_array);
                if ($result === false) {
                    show_message('Class:' . $class . ',Method:' . $function . ' Call Fail');
                }
                self::_output($result);
            }
        } else {
            show_message(str_replace(BASE_PATH, '', $path) . ' File Not Found...');
        }
    }

    /**
     * 输出参数
     * @param $result
     * @author: wzl
     * @date: 2017
     */
    private static function _output($result)
    {
        if(is_array($result) || is_object($result)) {
            echo json_encode($result);
        } else {
            echo $result;
        }
    }

    /**
     * 路径处理支持三种
     * 一种：http://localhost/ask/index.php?c=class&m=method
     * 二种：http://localhost/ask/class/method 通过s=class/method
     * 三种: $_SERVER['PATH_INFO']
     */
    private static function _dispatch()
    {
        $sep = C('_sep');
        $var_class = C('_var_class');
        $var_method = C('_var_method');
        $class = C('_class');
        $method = C('_method');
        $path_info = C('_var_path_info');
        if (isset($_GET[$var_class]) && isset($_GET[$var_method])) {
            $path = trim($_GET[$var_class]) . $sep . trim($_GET[$var_method]);
            unset($_GET[$var_class], $_GET[$var_method]);
        } else {
            //获取兼容模板 , 以/分隔            
            //第一种 $_SERVER['PATH_INFO'];
            if (isset($_SERVER['PATH_INFO'])) {
                $path = $_SERVER['PATH_INFO'];
            }//第二种　s模式
            elseif (isset($_GET[$path_info])) {
                $path = trim($_GET[$path_info]);
                unset($_GET[$path_info]);
            }
            //处理 /
            $path = trim($path, $sep);
            if (empty($path)) {
                $path = $class . $sep . $method;
            }
            //给个默认的function index(){}
            if (strpos($path, $sep) === false) {
                $path = $path . $sep . $method;
            }
            //处理假.html .htm            
            $path = str_replace(C('_vr_suffix'), '', $path);
        }
        define('__INFO__', $path);
        $_SERVER['PATH_INFO'] = __INFO__;
    }


    /**
     * 处理参数并转换成$_GET
     * @param $paths
     * @author: wzl
     * @date: 2017
     * @return array
     */
    private static function _doParams($paths)
    {
        $params_list = array_slice($paths, 2);
        if (count($params_list) >= 2) {
            $params = array_chunk($params_list, 2);
            foreach ($params as $get) {
                $_GET[$get[0]] = $get[1];
            }
        }
        return $params_list;
    }
}

